const App = getApp()

Page({

  data:{

    //菜品数据
    dish:[],

    //激活的菜品分类索引
    activeIndex:0,

    //菜品区域高度数组
    dishAreaHeightList:[],

    //当前菜品分类名称

    currentCategoryName:'推荐',

    scrollTop:0,

    //购物车数据
    cart:{
      totalCount:0,
      totalAmount:0,
      cartList:[]
    }
  },



  onLoad(){
   
    //获取菜品数据
    App.HttpService.dish()
      .then(res=>{
        this.setData({
          dish:res.data
        })

        this.setdishAreaHeightList()
      })
  },

  //计算菜品高度数组
  setdishAreaHeightList(){
    let dishAreaHeightList=[0]
    let dishHeight=0
    setTimeout(()=>{
      this.data.dish.forEach((item)=>{
        dishHeight+=item.dishes.length*106
        dishAreaHeightList.push(dishHeight)
      })

      this.setData({
        dishAreaHeightList:dishAreaHeightList
      })
    },500)
  },

  // 显示订单列表
  onShowOrderList(){
    App.WechatService.redirectTo('/pages/order/index')
  },

  //滚动事件监听
  onDishScroll(e){
    let scrollTop=e.detail.scrollTop
    let dishAreaHeightList = this.data.dishAreaHeightList

    dishAreaHeightList.forEach((item,index)=>{
      if(scrollTop>=dishAreaHeightList[index]&&scrollTop<dishAreaHeightList[index+1]){
        let categoryName=this.data.dish[index].categoryName
        this.setData({
          activeIndex:index,
          currentCategoryName:categoryName
        })
      }
    })
  },

  //菜品分类修改点击事件
  onCategoryChange(e){
    let currentIndex=e.detail
    this.setData({
      activeIndex:currentIndex,
      scrollTop:this.data.dishAreaHeightList[currentIndex]
    })
  },

  //商品添加按钮点击事件
  onAddCount(e){
    let item=e.currentTarget.dataset.item //当前菜品
    let categoryIndex=e.currentTarget.dataset.categoryindex //菜品分类索引
    let itemIndex = e.currentTarget.dataset.itemindex //菜品分类中的菜品索引

    item.categoryIndex=categoryIndex
    item.itemIndex=itemIndex

    let cart=this.data.cart
    if(item.count){
      item.count++
      cart.cartList.filter((cartItem,index)=>{
        if(item.dishId===cartItem.dishId){
          cartItem.count=item.count
        }
      })
    }else{
      item.count=1
      cart.cartList.push(item)
    }

    cart.totalCount=this.getCartTotalCount(cart.cartList)
    cart.totalAmount = this.getCartTotalAmount(cart.cartList)

    this.setData({
      cart,
      [`dish[${categoryIndex}].dishes[${itemIndex}].count`]:item.count
    })

  },

  //商品减少按钮点击事件
  onSubCount(e){

    let item=e.currentTarget.dataset.item //当前菜品
    let categoryIndex = e.currentTarget.dataset.categoryindex //菜品分类索引
    let itemIndex = e.currentTarget.dataset.itemindex //菜品分类中菜品索引
    
    item.categoryIndex = categoryIndex
    item.itemIndex=itemIndex

    let cart=this.data.cart
    item.count--
    cart.cartList.filter((cartItem,index)=>{
      if (item.dishId === cartItem.dishId){
        cartItem.count=item.count
        if(cartItem.count===0){
          cart.cartList.splice(index,1)
          item.count=null
        }
      }
    })

    cart.totalCount=this.getCartTotalCount(cart.cartList)
    cart.totalAmount=this.getCartTotalAmount(cart.cartList)
    this.setData({
      cart,
      
      [`dish[${categoryIndex}].dishes[${itemIndex}].count`]: item.count
    })
  },


  //计算购物车中的菜品的总数量
  getCartTotalCount(cartList){
    let totalCount=cartList.reduce((sum,item)=>{
      return sum+=item.count
    },0)

    return totalCount
  },

  //计算购物车中的总金额
  getCartTotalAmount(cartList){
    let totalAmount=cartList.reduce((sum,item)=>{
      return sum+=item.count*item.unitPrice
    },0)

    return totalAmount
  },

  // 开启购物车列表弹窗
  onCartPopupOpen(){
    let cart=this.data.cart
    if(cart.totalCount>0){
      this.setData({
        showCartPopup:true
      })
    }
  },

  // 关闭购物车列表弹窗
  onCartPopupClose(){
    this.setData({
      showCartPopup:false
    })
  },

  // 添加购物车中的商品数量
  onAddCartCount(e){
    let cart = this.data.cart
    let cartItem = e.currentTarget.dataset.cartitem
    let cartIndex = e.currentTarget.dataset.cartindex
    let categoryIndex = cartItem.categoryIndex
    let itemIndex = cartItem.itemIndex

    cartItem.count++
    cart.cartList[cartIndex] = cartItem

    cart.totalCount=this.getCartTotalCount(cart.cartList)
    cart.totalAmount=this.getCartTotalAmount(cart.cartList)

    this.setData({
      cart,
      [`dish[${categoryIndex}].dishes[${itemIndex}].count`]:cartItem.count
    })
  },

  // 减少购物车中的商品数量
  onSubCartCount(e){
    let cart=this.data.cart
    let cartItem = e.currentTarget.dataset.cartitem
    let cartIndex = e.currentTarget.dataset.cartindex
    let categoryIndex = cartItem.categoryIndex
    let itemIndex = cartItem.itemIndex

    cartItem.count--
    cart.cartList[cartIndex]=cartItem
    if(cartItem.count===0){
      cart.cartList.splice(cartIndex,1)
    }

    cart.totalCount=this.getCartTotalCount(cart.cartList)
    cart.totalAmount=this.getCartTotalAmount(cart.cartList)

    let dishCount = cartItem.count===0?null:cartItem.count

    this.setData({
      cart,
      [`dish[${categoryIndex}].dishes[${itemIndex}].count`]:dishCount
    })

    if(cart.totalcount===0){
      this.setData({
        showCartPopup:false
      })
    }
  },

  // 清空购物车中的所有商品
  onClearCart(){
    let cart=this.data.cart

    cart.totalCount=0
    cart.totalAmount=0
    cart.cartList.forEach((item,index)=>{
      this.setData({
        [`dish[${item.categoryIndex}].dishes[${item.itemIndex}].count`]:null
      })
    })
    cart.cartList=[],
    this.setData({
      cart,
      showCartPopup:false
    })
  },

  // 订单提交
  onOrderSubmit(){
    
    let cart=this.data.cart
    App.HttpService.orderSubmit(cart)
    .then(res=>{
      let orderId=res.data
      
      App.WechatService.redirectTo('/pages/pay/index?orderId='+orderId)
    })
  }

})